package com.shenfa.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import com.shenfa.bean.CostPar;
import com.shenfa.service.CostParService;

import com.shenfa.util.ResultObject;

/**
 * 计费信息管理Action
 * 
 * @author yanglong
 * 
 * @date 2011-8-23
 *
 */
@SuppressWarnings("serial")
public class CostParAction extends BaseAction {

	@Autowired
	private CostParService costParService;
	private HttpServletResponse response;
	
	private static Logger log=Logger.getLogger(CostParAction.class);
	private CostPar costPar;
	private int start;
	private int limit;
	/**
	 * 根据计费对象查询计费列表
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String list() {
		log.debug("costPar="+costPar+",start="+start+",limit="+limit);
		List<CostPar> list = costParService.getList(costPar, start, limit);
		result = ResultObject.build("0", list);
		result.put("total", costParService.queryCount(costPar));
		return SUCCESS;
	}

	/**
	 * 添加计费信息
	 * @return
	 */
	public String add() {
		log.debug("cityNo="+costPar.getCityno()+",epibolyNo="+costPar.getEpibolyno()+",cityName="+costPar.getCityname()+",envcost="+costPar.getEnvcost()
				+",envelop="+costPar.getEnvelop()+",envlopWeight="+costPar.getEnvlopweight()+",epibolyName="+costPar.getEpibolyname()+",print="+costPar.getPrint()
				+",reckoning="+costPar.getReckoning()+",totalWeight="+costPar.getTotalweight());
		int type = costParService.add(costPar);
		if(type == 1){
			result = ResultObject.build(String.valueOf(type), "添加成功！");
		} else {
			result = ResultObject.build(String.valueOf(type), "操作失败！");
		}
		return SUCCESS;
	}

	/**
	 * 删除计费信息
	 * @return
	 */
	public String del() {
		log.debug("cityNo="+costPar.getCityno()+",epibolyNo="+costPar.getEpibolyno());
		int type = costParService.del(costPar);
		if(type == 1){
			result = ResultObject.build(String.valueOf(type), "删除成功！");
		} else {
			result = ResultObject.build(String.valueOf(type), "操作失败！");
		}
		return SUCCESS;
	}

	/**
	 * 更新计费信息
	 * @return
	 */
	public String upd() {
		log.debug("cityNo="+costPar.getCityno()+",epibolyNo="+costPar.getEpibolyno()+",cityName="+costPar.getCityname()+",envcost="+costPar.getEnvcost()
				+",envelop="+costPar.getEnvelop()+",envlopWeight="+costPar.getEnvlopweight()+",epibolyName="+costPar.getEpibolyname()+",print="+costPar.getPrint()
				+",reckoning="+costPar.getReckoning()+",totalWeight="+costPar.getTotalweight());
		int type = costParService.upd(costPar);
		if(type >= 0){
			result = ResultObject.build("0", null);
		} else {
			result = ResultObject.build("1", null);
		}
		return SUCCESS;
	}
	
	public String battchUpdate(){
		int type=0;
		if(costPar.getCityno().contains(",")){
			String[] cityArray=costPar.getCityno().split(",");
			for (int i=0;i<cityArray.length;i++) {
				costPar.setCityno(cityArray[i]);
				type+=costParService.upd(costPar);
			}
		}else{
			type=costParService.upd(costPar);
		}
		
		response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter pw;
		try {
			pw = response.getWriter();
			pw.print("{success:true,total:'"+type+"'}");
			pw.flush();
			pw.close();
			response.flushBuffer();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}

	public CostPar getCostPar() {
		return costPar;
	}

	public void setCostPar(CostPar costPar) {
		this.costPar = costPar;
	}
	
	public void setStart(int start) {
		this.start = start;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	
}
